package com.ag.improvedjava.iterable;

import java.util.Iterator;

import com.ag.improvedjava.utils.Function;

final class SelectIterable<T, R> implements Iterable<R> {

	private Iterable<T> iterable;
	private Function<T, R> function;
	
	SelectIterable(Iterable<T> iterable, Function<T, R> function) {
		this.iterable = iterable;
		this.function = function;
	}

	@Override
	public Iterator<R> iterator() {
		return new SelectIterator<>(iterable.iterator(), function);
	}
}

class SelectIterator<T, R> extends UnmodifiableIterator<R> {
	
	private Iterator<T> iterator;
	private Function<T, R> function;
	
	SelectIterator(Iterator<T> iterator, Function<T, R> function) {
		this.iterator = iterator;
		this.function = function;
	}

	@Override
	public boolean hasNext() {
		return iterator.hasNext();
	}

	@Override
	public R next() {
		return function.execute(iterator.next());
	}

}
